package com.md.api.controller;

import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
import com.github.binarywang.wxpay.service.WxPayService;
import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.enums.OrderType;
import com.md.api.service.OrderService;
import com.md.api.service.RechargeService;
import com.md.api.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

	@Autowired
	private WxPayService wxService;
	@Resource
    OrderService orderSevice;
	@Autowired
	ShoppingCartService shoppingCartService;
	@Autowired
	RechargeService rechargeService;



	@RequestMapping("/myShoppingOrder")
	@ResponseBody
	public HttpResult myShoppingOrder(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {

		return orderSevice.myShoppingOrder(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/deleteOrder")
	@ResponseBody
	public HttpResult deleteOrder(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {

		return orderSevice.deleteOrder(request,qto);
	}



	@RequestMapping("/shoppingOrderDetail")
	@ResponseBody
	public HttpResult shoppingOrderDetail(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {

		return orderSevice.shoppingOrderDetail(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/confirmShoppingOrder")
	@ResponseBody
	public HttpResult confirmShoppingOrder(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {

		return orderSevice.confirmShoppingOrder(request,qto);
	}



	@RequestMapping("/queryOrder")
	public HttpResult queryOrder(@RequestBody ComposeQueryDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())||null==qto.getOrderType()){
			return HttpResult.PARAM_ERROR;
		}
		try {
			// 创建查询请求对象
			WxPayOrderQueryRequest queryRequest = new WxPayOrderQueryRequest();
			queryRequest.setOutTradeNo(qto.getOrderNo()); // 设置你需要查询的订单号
			// 调用查询方法
			WxPayOrderQueryResult result = wxService.queryOrder(queryRequest);
			log.info("手动查询微信支付订单返回："+result.toString());
			// 根据查询结果处理你的业务逻辑
			if (qto.getOrderType()== OrderType.SHOPPING.value()){
				shoppingCartService.successPay(qto.getOrderNo(),result.getTotalFee(), result.getTradeState());
			}else if (qto.getOrderType()== OrderType.RECHARGE.value()){
				rechargeService.successPay(qto.getOrderNo(),result.getTotalFee(), result.getTradeState());
			}
			return new HttpResult(result);
		}catch (Exception e){
			e.printStackTrace();
		}
		return HttpResult.fail("查询失败！");
	}


	@RequestMapping("/queryRefundOrder")
	public HttpResult queryRefundOrder(@RequestBody ComposeQueryDto qto) {
		if (StringUtils.isEmpty(qto.getOrderNo())){
			return HttpResult.PARAM_ERROR;
		}
		try {
			// 创建查询请求对象
			WxPayRefundQueryRequest queryRequest = new WxPayRefundQueryRequest();
			queryRequest.setOutTradeNo(qto.getOrderNo()); // 设置你需要查询的订单号
			// 调用查询方法
			WxPayRefundQueryResult result = wxService.refundQuery(queryRequest);
			log.info("手动查询微信退款订单返回："+result.toString());
//			// 根据查询结果处理你的业务逻辑
//			if (qto.getOrderType()== OrderType.DRINKING.value()){
//				drinkingCartService.successPay(qto.getOrderNo(),result.getTotalFee(), result.getTradeState());
//			}else if (qto.getOrderType()== OrderType.SHOPPING.value()){
//				shoppingCartService.successPay(qto.getOrderNo(),result.getTotalFee(), result.getTradeState());
//			}else if (qto.getOrderType()== OrderType.RECHARGE.value()){
//				rechargeService.successPay(qto.getOrderNo(),result.getTotalFee(), result.getTradeState());
//			}
			return new HttpResult(result);
		}catch (Exception e){
			e.printStackTrace();
		}
		return HttpResult.fail("查询失败！");
	}
}
